package com.software.controller;

import com.software.base.ResultInfo;
import com.software.bean.AdminBean;
import com.software.dto.*;
import com.software.service.AdminService;
import com.software.utils.AssertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * @BelongsProject: Second-hand-trading-platform
 * @BelongsPackage: com.software.controller
 * @Author: wyj
 * @CreateTime: 2024-02-27  15:04
 * @Description:
 * @Version: 1.0
 */
@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    /**
     * @description:获取管理员信息
     * @author: wyj
     * @date: 2024/4/9 20:07
     * @param:
     * @return:
     */
    @GetMapping("/api/getAminInfo/{id}")
    @ResponseBody
    public ResultInfo getAdminInfo(@PathVariable Integer id) {
        AdminBean adminBean = adminService.selectById(id);
        return ResultInfo.success(adminBean);
    }

    @PostMapping("/api/login")
    @ResponseBody
    public ResultInfo adminLogin(@RequestBody LoginDto loginDto, HttpServletRequest request) {
        return adminService.adminLogin(loginDto, request);
    }

    /**
     * @description:退出
     * @author: wyj
     * @date: 2024/4/9 19:14
     * @param:
     * @return:
     */
    @PostMapping("/api/logout")
    @ResponseBody
    public ResultInfo adminLogout() {
        return ResultInfo.success();
    }

    /**
     * @description:修改管理员头像
     * @author: wyj
     * @date: 2024/4/9 19:15
     * @param:
     * @return:
     */
    @PostMapping("/api/updateAdminAvatar")
    @ResponseBody
    public ResultInfo updateAdminAvatar(AdminDto adminDto) {
        Integer result = adminService.updateAdminAvatar(adminDto);
        AssertUtil.isTrue(result != 1, "修改失败");
        return ResultInfo.success();
    }

    /**
     * @description:更新个人信息
     * @author: wyj
     * @date: 2024/4/16 20:29
     * @param:
     * @return:
     */
    @PutMapping("/api/updateAdminInfo")
    @ResponseBody
    public ResultInfo updateAdminInfo(@RequestBody UpdateAdminDto updateAdminDto) {
        Integer result = adminService.updateAdminInfo(updateAdminDto);
        return ResultInfo.success();
    }

    /**
     * @description:修改密码
     * @author: wyj
     * @date: 2024/4/16 20:29
     * @param:
     * @return:
     */
    @PutMapping("/api/updatePassword")
    @ResponseBody
    public ResultInfo updatePassword(@RequestBody UpdateAdminPasswordDto updateAdminDto) {
        Integer result = adminService.updatePassword(updateAdminDto);
        AssertUtil.isTrue(result != 1, "更新失败");
        return ResultInfo.success();
    }

    @GetMapping("/api/getAdminList")
    @ResponseBody
    public ResultInfo getAdminList() {
        List<AdminBean> adminBeanList = adminService.getAdminList() ;
        return ResultInfo.success(adminBeanList);
    }

    /** @description:添加管理员
      * @author: wyj
      * @date: 2024/4/16 21:47
      * @param:
      * @return:
      */
    @PostMapping("/api/addAdmin")
    @ResponseBody
    public ResultInfo addAdmin(@RequestBody AddAdminDto addAdminDto) {
        Integer result = adminService.addAdmin(addAdminDto);
        AssertUtil.isTrue(result != 1, "添加失败");
        return ResultInfo.success();
    }

    /** @description:禁用管理员账号
      * @author: wyj
      * @date: 2024/4/16 21:47
      * @param:
      * @return:
      */
    @PutMapping("/api/disableAdmin/{id}")
    @ResponseBody
    public ResultInfo disableAdmin(@PathVariable Integer id) {
        Integer result = adminService.disableAdmin(id);
        AssertUtil.isTrue(result != 1, "禁用失败");
        return ResultInfo.success();
    }

    /** @description:启用管理员账户
      * @author: wyj
      * @date: 2024/4/16 21:49
      * @param:
      * @return:
      */

    @PutMapping("/api/enableAdmin/{id}")
    @ResponseBody
    public ResultInfo enableAdmin(@PathVariable Integer id) {
        Integer result = adminService.enableAdmin(id);
        AssertUtil.isTrue(result != 1, "启用失败");
        return ResultInfo.success();
    }

}

